package com.controller.developerPortal;

import java.util.List;
import java.util.Map;

import model.Document;
import model.DocumentType;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import dao.DocumentDAO;
import dao.DocumentTypeDAO;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/data/")
public class DataController {
	
	ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("springConfigSQL.xml");
	DocumentTypeDAO documentTypeDAO = ctx.getBean("documentTypeDAO",DocumentTypeDAO.class);
	DocumentDAO documentDAO = ctx.getBean("documentDAO",DocumentDAO.class);


	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index() {
		return "dataDoc";
	}
	
	@RequestMapping(value = "/read", method = RequestMethod.GET)
	public @ResponseBody List<DocumentType> read(){
		return documentTypeDAO.getAll();
	}
	
	 @RequestMapping(value = "/update", method = RequestMethod.POST)
	    public @ResponseBody DocumentType update(@RequestBody Map<String, Object> model) {
	        System.out.println("You reached here!");
	    	DocumentType target = new DocumentType();
	    	int typeId = Integer.parseInt(model.get("typeId").toString()) ;
	    	String typeName =  model.get("typeName").toString();
	    	String status =  model.get("status").toString();
	    	
	    	target.setTypeId(typeId);
	    	target.setTypeName(typeName);
	    	target.setStatus(status);
	    	
	    	System.out.println("Update here "+target.toString());
	    	documentTypeDAO.update(target);
	        System.out.println("Updated!");
	        return target;
	    }
	    
	    @RequestMapping(value = "/create", method = RequestMethod.POST)
	    public @ResponseBody DocumentType create(@RequestBody Map<String, Object> model) {
	    	DocumentType target = new DocumentType();

	    	int typeId = Integer.parseInt(model.get("typeId").toString()) ;
	    	String typeName = (String) model.get("typeName");
	    	String status = (String) model.get("status");
	    	
	    	System.out.println("typeid" + typeId + "typeName" +typeName +"status " + status);
	    	
	    	target.setTypeId(typeId);
	    	target.setTypeName(typeName);
	    	target.setStatus(status);  
	        
	        documentTypeDAO.save(target);
	        
	        return target;
	    }
	    
	    @RequestMapping(value = "/destroy", method = RequestMethod.POST)
	    public @ResponseBody DocumentType destroy(@RequestBody Map<String, Object> model) {
	    	DocumentType target = new DocumentType();
	        
	    	target.setTypeId((Integer)model.get("typeId"));
	        
	        documentTypeDAO.deleteById(target.getTypeId());
	        
	        return target;
	    }  
	    
	    @RequestMapping(value = "/readDoc", method = RequestMethod.GET)
		public @ResponseBody List<Document> readDoc(){
			return documentDAO.getAll();
		}
	    
		 @RequestMapping(value = "/updateDoc", method = RequestMethod.POST)
		    public @ResponseBody Document updateDoc(@RequestBody Map<String, Object> model) {
		        System.out.println("You reached here!");
		        Document target = new Document();
//		        `DocId`, `DocName`, `TypeId`, `OriginalDate`, `NotarizedDate`, `Description`, `Status`
		        int docId = Integer.parseInt(model.get("docId").toString());
		        String docName = model.get("docName").toString();
		    	int typeId = Integer.parseInt(model.get("typeId").toString()) ;
		    	String originalDate = model.get("originalDate").toString();
		    	String notarizedDate = model.get("notarizedDate").toString();
		    	String description = model.get("description").toString();
		    	String status =  model.get("status").toString();
		    	int numberOfDownload = Integer.parseInt(model.get("numberOfDownload").toString());
		    	String filePath = model.get("filePath").toString();

		    	target.setDocId(docId);
		    	target.setDocName(docName);
		    	target.setTypeId(typeId);
		    	target.setOriginalDate(originalDate);
		    	target.setNotarizedDate(notarizedDate);
		    	target.setDescription(description);
		    	target.setStatus(status);
		    	target.setNumberOfDownload(numberOfDownload);
		    	target.setFilePath(filePath);
		    	
		    	System.out.println("Update here "+target.toString());
		    	documentDAO.update(target);
		        return target;
		    }
		    
		    @RequestMapping(value = "/createDoc", method = RequestMethod.POST)
		    public @ResponseBody Document createDoc(@RequestBody Map<String, Object> model) {
		    	Document target = new Document();

		    	 int docId = Integer.parseInt(model.get("docId").toString());
			        String docName = model.get("docName").toString();
			    	int typeId = Integer.parseInt(model.get("typeId").toString()) ;
			    	String originalDate = model.get("originalDate").toString();
			    	String notarizedDate = model.get("notarizedDate").toString();
			    	String description = model.get("description").toString();
			    	String status =  model.get("status").toString();
			    	int numberOfDownload = Integer.parseInt(model.get("numberOfDownload").toString());
			    	String filePath = model.get("filePath").toString();

			    	target.setDocId(docId);
			    	target.setDocName(docName);
			    	target.setTypeId(typeId);
			    	target.setOriginalDate(originalDate);
			    	target.setNotarizedDate(notarizedDate);
			    	target.setDescription(description);
			    	target.setStatus(status);
			    	target.setNumberOfDownload(numberOfDownload);
			    	target.setFilePath(filePath);
		        
		        documentDAO.save(target);
		        
		        return target;
		    }
		    
		    @RequestMapping(value = "/destroyDoc", method = RequestMethod.POST)
		    public @ResponseBody Document destroyDoc(@RequestBody Map<String, Object> model) {
		    	Document target = new Document();
		        
		    	target.setDocId(Integer.parseInt(model.get("docId").toString()));	
		    	System.out.println(target.getDocId());
		        documentDAO.deleteById(target.getDocId());
		        return target;
		    }  
}
